/**
 * @Project NUKEVIET 4.x
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2014 VINADES.,JSC. All rights reserved
 * @License GNU/GPL version 2 or any later version
 * @Createdate 27/01/2011, 9:36
 */

function htmlspecialchars_decode(string, quote_style) {
    //       discuss at: http://phpjs.org/functions/htmlspecialchars_decode/
    //      original by: Mirek Slugen
    //      improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    //      bugfixed by: Mateusz "loonquawl" Zalega
    //      bugfixed by: Onno Marsman
    //      bugfixed by: Brett Zamir (http://brett-zamir.me)
    //      bugfixed by: Brett Zamir (http://brett-zamir.me)
    //         input by: ReverseSyntax
    //         input by: Slawomir Kaniecki
    //         input by: Scott Cariss
    //         input by: Francois
    //         input by: Ratheous
    //         input by: Mailfaker (http://www.weedem.fr/)
    //       revised by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // reimplemented by: Brett Zamir (http://brett-zamir.me)
    //        example 1: htmlspecialchars_decode("<p>this -&gt; &quot;</p>", 'ENT_NOQUOTES');
    //        returns 1: '<p>this -> &quot;</p>'
    //        example 2: htmlspecialchars_decode("&amp;quot;");
    //        returns 2: '&quot;'

    var optTemp = 0,
        i = 0,
        noquotes = false;
    if (typeof quote_style === 'undefined') {
        quote_style = 2;
    }
    string = string.toString()
        .replace(/&lt;/g, '<')
        .replace(/&gt;/g, '>');
    var OPTS = {
        'ENT_NOQUOTES': 0,
        'ENT_HTML_QUOTE_SINGLE': 1,
        'ENT_HTML_QUOTE_DOUBLE': 2,
        'ENT_COMPAT': 2,
        'ENT_QUOTES': 3,
        'ENT_IGNORE': 4
    };
    if (quote_style === 0) {
        noquotes = true;
    }
    if (typeof quote_style !== 'number') {
        // Allow for a single string or an array of string flags
        quote_style = [].concat(quote_style);
        for (i = 0; i < quote_style.length; i++) {
            // Resolve string input to bitwise e.g. 'PATHINFO_EXTENSION' becomes 4
            if (OPTS[quote_style[i]] === 0) {
                noquotes = true;
            } else if (OPTS[quote_style[i]]) {
                optTemp = optTemp | OPTS[quote_style[i]];
            }
        }
        quote_style = optTemp;
    }
    if (quote_style & OPTS.ENT_HTML_QUOTE_SINGLE) {
        string = string.replace(/&#0*39;/g, "'"); // PHP doesn't currently escape if more than one 0, but it should
        // string = string.replace(/&apos;|&#x0*27;/g, "'"); // This would also be useful here, but not a part of PHP
    }
    if (!noquotes) {
        string = string.replace(/&quot;/g, '"');
    }
    // Put this in last place to avoid escape being double-decoded
    string = string.replace(/&amp;/g, '&');

    return string;
}

function nv_filename_alt( fileAlt ){
    var lastChar = fileAlt.charAt(fileAlt.length - 1);

    if( lastChar === '/' || lastChar === '\\' ){
        fileAlt = fileAlt.slice(0, -1);
    }

    fileAlt = decodeURIComponent( htmlspecialchars_decode( fileAlt.replace(/^.*[\/\\]/g, '') ) );
    fileAlt = fileAlt.split('.');
    
    if( fileAlt.length > 1 ){
    	fileAlt[fileAlt.length - 1] = '';
    }
    
    fileAlt = fileAlt.join(' ');
    fileAlt = fileAlt.split('_');
    fileAlt = fileAlt.join(' ');
    fileAlt = fileAlt.split('-');
    fileAlt = fileAlt.join(' ');
    return trim( fileAlt );
}

function nv_randomNum( a ){
	for( var b = "", d = 0; d < a; d++ ){
		b += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" . charAt( Math.floor( Math.random() * 62 ) );
	}
	
	return b;
}

// Return height
function resize_byWidth( a, b, d ){
	return parseInt( Math.round( d / a * b ) );
}

// Return width
function resize_byHeight( a, b, d ){
	return parseInt( Math.round( d / b * a ) );
}

// Resize image with display width and height (fix to container)
function calSize( a, b, d, e ){
	if( a > d ){
		b = resize_byWidth( a, b, d );
		a = d;
	}
	
	if( b > e ){
		a = resize_byHeight( a, b, e );
		b = e
	}
	
	return [parseInt( a ), parseInt( b )];
}

function calSizeMax( a, b, d, e ){
	var g = d;
	d = resize_byWidth( a, b, d );
	
	if( ! ( d <= e ) ){
		d = e;
		g = resize_byHeight( a, b, e );
	}
	
	return [parseInt( g ), parseInt( d )];
}

function calSizeMin( a, b, d, e ){
	var g = d;
	d = resize_byWidth( a, b, d );
	
	if( ! ( d >= e ) ){
		d = e;
		g = resize_byHeight( a, b, e );
	}
	
	return [parseInt( g ), parseInt( d )];
}

function is_numeric(a){
	return ( typeof a === "number" || typeof a === "string" ) && a !== "" && !isNaN(a);
}

function checkNewSize(){
	var a = $("input[name=newWidth]").val(), b = $("input[name=newHeight]").val(), d = [], e = $("input[name=origWidth]").val(), g = $("input[name=origHeight]").val(), h = calSizeMax(e, g, nv_max_width, nv_max_height);
	e = calSizeMin(e, g, nv_min_width, nv_min_height);
	if(a == "" || !is_numeric(a)){
		d = [LANG.errorEmptyX, "newWidth"];
	} else {
		if(a > h[0]){
			d = [LANG.errorMaxX, "newWidth"];
		} else {
			if(a < e[0]){
				d = [LANG.errorMinX, "newWidth"];
			} else {
				if(b == "" || !is_numeric(b)){
					d = [LANG.errorEmptyY, "newHeight"];
				} else {
					if(b > h[1]){
						d = [LANG.errorMaxY, "newHeight"];
					} else {
						if(b < e[1]){
							d = [LANG.errorMinY, "newHeight"];
						}
					}
				}
			}
		}
	}
	$("div[title=createInfo]").find("div").remove();
	if( typeof d[0] != "undefined"){
		$("div[title=createInfo]").prepend('<div class="red">' + d[0] + "</div>");
		$("input[name='" + d[1] + "']").select();
		return false;
	}
	a = calSize(a, b, 360, 230);
	$("img[name=myFile2]").width(a[0]).height(a[1]);
	return true;
}

function pathList(a, b){
	var d = [];
	$("#foldertree").find("span").each(function(){
		if($(this).attr("title") == b || $(this).attr("title") != "" && $(this).is("." + a)){
			d[d.length] = $(this).attr("title");
		}
	});
	return d;
}

// Dong cua so va tra ve gia tri
function insertvaluetofield(){
	var area = $("input[name=area]").val();
	var selFile = $("input[name=selFile]").val();
	var imageInfo = $("img[title='" + selFile + "']").attr("name").split("|");
	var path = ( imageInfo[7] == "" ) ? $("span#foldervalue").attr("title") : imageInfo[7];
	var fullPath = nv_siteroot + path + "/" + selFile;
	
	if( area != '' ){
		$("#" + area, opener.document).val( fullPath );
		
		var idalt = $("input[name=alt]").val();
		if( idalt != '' ) {
		    fileAlt = nv_filename_alt( selFile );
		    alt = $("img[title='" + selFile + "']").attr("alt");
			if( alt == fileAlt ){
				alt = '';
			}
			$("#" + idalt, opener.document).val( alt );
		}
		window.close();
	}else{
		var CKEditorFuncNum = $("input[name=CKEditorFuncNum]").val();
		
		window.opener.CKEDITOR.tools.callFunction(CKEditorFuncNum, fullPath, function(){
			var dialog = this.getDialog();
			
			if(dialog.getName() == 'image2'){
				var element = dialog.getContentElement('info', 'alt');
				
				if( element ){
					element.setValue( $("img[title='" + selFile + "']").attr("alt") );
				}
			}
		});

		window.close();
	}
}

// Tai file ve
function download(){
	var selFile = $("input[name=selFile]").val();
	var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
	
	fullPath = (selFileData[7] == "") ? $("span#foldervalue").attr("title") : selFileData[7];
	
	$("iframe#Fdownload").attr("src", nv_module_url + "dlimg&path=" + fullPath + "&img=" + selFile);
}

// Xem thong tin chi tiet
function preview(){
	$("div.dynamic").text("");
	$("input.dynamic").val("");
	
	var selFile = $("input[name=selFile]").val();
	var html = LANG.upload_size + ": ";
	
	var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
	fullPath = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
	
	if( selFileData[3] == "image" || selFileData[2] == "swf" ){
		var size = calSize( selFileData[0], selFileData[1], 360, 230 );
		html += selFileData[0] + " x " + selFileData[1] + " pixels (" + selFileData[4] + ")<br />";
		selFileData[3] == "image" ? $("div#fileView").html('<img width="' + size[0] + '" height="' + size[1] + '" src="' + nv_siteroot + fullPath + "/" + selFile + '?' + selFileData[8] + '" />') : $("#fileView").flash({
			src : nv_siteroot + fullPath + "/" + selFile,
			width : size[0],
			height : size[1]
		}, {
			version : 8
		});
	}else{
		html += selFileData[4] + "<br />";
		$("div#fileView").html( $("div[title='" + selFile + "'] div").html() );
	}
	
	html += LANG.pubdate + ": " + selFileData[6];
	
	$("#fileInfoAlt").html( $("img[title='" + selFile + "']").attr("alt") );
	$("#fileInfoDetail").html( html );
	$("#fileInfoName").html( selFile );
	
	$("div#imgpreview").dialog({
		autoOpen : false,
		width : 388,
		modal : true,
		position : "center"
	}).dialog("open").dblclick(function(){
		$("div#imgpreview").dialog("close");
	});
}

// Tao anh moi (Menu cong cu anh)
function create(){
	$("div.dynamic").text("");
	$("input.dynamic").val("");
	
	var selFile = $("input[name=selFile]").val();
	selFileData = $("img[title='" + selFile + "']").attr("name");
	selFileData = selFileData.split("|");
	
	if( selFileData[3] == "image" ){
		path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
		
		$("input[name=origWidth]").val( selFileData[0] );
		$("input[name=origHeight]").val( selFileData[1] );
		
		var SizeMax = calSizeMax( selFileData[0], selFileData[1], nv_max_width, nv_max_height );
		var SizeMin = calSizeMin( selFileData[0], selFileData[1], nv_min_width, nv_min_height );
		
		$("div[title=createInfo]").html("Max: " + SizeMax[0] + " x " + SizeMax[1] + ", Min: " + SizeMin[0] + " x " + SizeMin[1] + " (pixels)");
		
		DisSize = calSize( selFileData[0], selFileData[1], 360, 230 );
		
		$("img[name=myFile2]").width( DisSize[0] ).height( DisSize[1] ).attr( "src", nv_siteroot + path + "/" + selFile + "?" + selFileData[8] );
		$("#fileInfoDetail2").html(LANG.origSize + ": " + selFileData[0] + " x " + selFileData[1] + " pixels");
		$("#fileInfoName2").html( selFile );
		
		$("div#imgcreate").dialog({
			autoOpen : false,
			width : 650,
			modal : true,
			position : "center"
		}).dialog("open");
	}
}

// Di chuyen file
function move(){
	$("div.dynamic").text("");
	$("input.dynamic").prop("checked", false);
	$("select[name=newPath]").html("");
	
	var selected, e;
	var currentFolder = $("span#foldervalue").attr("title");
	var listPath = pathList("create_file", currentFolder);
	
	for( e in listPath ){
		selected = currentFolder == listPath[e] ? ' selected="selected"' : "";
		$("select[name=newPath]").append('<option value="' + listPath[e] + '"' + selected + ">" + listPath[e] + "</option>");
	}
	
	var selFile = $("input[name=selFile]").val();

	// Kiem tra di chuyen nhieu file hay di chuyen 1 file
	if( selFile.indexOf('|') == -1 ){
		var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
		var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
		var moveMessage = path + "/" + selFile;
	}else{
		selFile = selFile.split('|');
		var selFileData = $("img[title='" + selFile[0] + "']").attr("name").split("|");
		var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
		var moveMessage = LANG.move_multiple.replace( '%s', selFile.length ) + ".";
		selFile = selFile.join('|');	
	}
	
	$("div[title=pathFileName]").text( moveMessage );
	$("div#filemove").dialog({
		autoOpen : false,
		width : 300,
		modal : true,
		position : "center"
	}).dialog("open");
}

// Doi ten file
function filerename(){
	$("div.dynamic, span.dynamic").text("");
	$("input.dynamic").val("");
	
	var a = $("input[name=selFile]").val();
	
	$("div#filerenameOrigName").text(a);
	$("input[name=filerenameNewName]").val(a.replace(/^(.+)\.([a-zA-Z0-9]+)$/, "$1"));
	$("span[title=Ext]").text("." + a.replace(/^(.+)\.([a-zA-Z0-9]+)$/, "$2"));
	$("input[name=filerenameAlt]").val($("img[title='" + a + "']").attr("alt"));
	
	$("div#filerename").dialog({
		autoOpen : false,
		width : 400,
		modal : true,
		position : "center"
	}).dialog("open");
}

// Goi chuc nang xoa file
function filedelete(){
	var imgtype = $("select[name=imgtype]").val();
	var author = $("select[name=author]").val() == 1 ? "&author" : "";
	var order = $("select[name=order]").val();
	
	var selFile = $("input[name=selFile]").val();
	
	// Kiem tra xoa nhieu file hay xoa 1 file
	if( selFile.indexOf('|') == -1 ){
		var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
		var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
		var confirmMessage = LANG.upload_delimg_confirm + " " + selFile + " ?";
	}else{
		selFile = selFile.split('|');
		var selFileData = $("img[title='" + selFile[0] + "']").attr("name").split("|");
		var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
		var confirmMessage = LANG.upload_delimgs_confirm.replace( '%s', selFile.length ) + "?";
		selFile = selFile.join('|');	
	}
	
	if( confirm( confirmMessage ) ){
		$.ajax({
			type : "POST",
			url : nv_module_url + "delimg",
			data : "path=" + path + "&file=" + selFile,
			success : function(e){
				e = e.split('#');
				
				if( e[0] != 'OK' ){
					alert( e[1] );
				}else{
					$("#imglist").html(nv_loading_data).load(nv_module_url + "imglist&path=" + path + "&type=" + imgtype + author + "&order=" + order + "&num=" + +nv_randomNum(10));
				}
			}
		});
	}
}

// Ham xu ly khi nhap chuot vao 1 file (Chuot trai, chuot giua lan chuot phai)
function fileMouseup( file, e ){
	// Khong xu ly neu jquery UI selectable dang kich hoat
	if( KEYPR.isFileSelectable == false ){
		// Set shift offset
		if( e.which != 3 && ! KEYPR.isShift ){
			// Reset shift offset
			KEYPR.shiftOffset = 0;
	
			$.each( $('.imgcontent'), function(k, v){
				if( v == file ){
					KEYPR.shiftOffset = k;
					return false;
				}
			});
		}
		
		// e.which: 1: Left Mouse, 2: Center Mouse, 3: Right Mouse
		if( KEYPR.isCtrl ){
			if( $(file).is('.imgsel') && e.which != 3 ){
				$(file).removeClass('imgsel');
			}else{
				$(file).addClass('imgsel');
			}
		}else if( KEYPR.isShift && e.which != 3 ){
			var clickOffset = -1;
			$('.imgcontent').removeClass('imgsel');
			
			$.each( $('.imgcontent'), function(k, v){
				if( v == file ){
					clickOffset = k;
				}
				
				if( ( clickOffset == -1 && k >= KEYPR.shiftOffset ) || ( clickOffset != -1 && k <= KEYPR.shiftOffset ) || v == file ){
					if( ! $(v).is('.imgsel') ){
						$(v).addClass('imgsel');
					}
				}
			});
		}else{
			if( e.which != 3 || ( e.which == 3 && ! $(file).is('.imgsel') ) ){
				$('.imgsel').removeClass('imgsel');
				$(file).addClass('imgsel');
			}
		}
		
		LFILE.setSelFile();
	
		if( e.which == 3 ){
			var isMultiple = $('.imgsel').length === 1 ? false : true;
			var fileExt = $("input[name=selFile]").val().slice(-3);	
			var CKEditorFuncNum = $("input[name=CKEditorFuncNum]").val();
			var area = $("input[name=area]").val();
			var html = "";
			
			if( ( CKEditorFuncNum > 0 || area != "" ) && ! isMultiple ){
				html += '<li id="select"><em class="fa fa-lg ' + ICON.select + '">&nbsp;</em>' + LANG.select + '</li>';
			}
			
			if( ! isMultiple ){
				html += '<li id="download"><em class="fa fa-lg ' + ICON.download + '">&nbsp;</em>' + LANG.download + '</li>';
				html += '<li id="filepreview"><em class="fa fa-lg ' + ICON.preview + '">&nbsp;</em>' + LANG.preview + '</li>';
			}
			
			if( $.inArray( fileExt, array_images ) !== -1 ){
				if( $("span#create_file").attr("title") == "1" && ! isMultiple ){
					html += '<li id="fileaddlogo"><em class="fa fa-lg ' + ICON.addlogo + '">&nbsp;</em>' + LANG.addlogo + '</li>';
					html += '<li id="create"><em class="fa fa-lg ' + ICON.create + '">&nbsp;</em>' + LANG.upload_createimage + '</li>';
					html += '<li id="cropfile"><em class="fa fa-lg ' + ICON.filecrop + '">&nbsp;</em>' + LANG.crop + '</li>';
					html += '<li id="rotatefile"><em class="fa fa-lg ' + ICON.filerotate + '">&nbsp;</em>' + LANG.rotate + '</li>';
				}
			}
			
			if( $("span#move_file").attr("title") == "1" ){
				html += '<li id="move"><em class="fa fa-lg ' + ICON.move + '">&nbsp;</em>' + LANG.move + '</li>';
			}
			
			if( $("span#rename_file").attr("title") == "1" && ! isMultiple ){
				html += '<li id="rename"><em class="fa fa-lg ' + ICON.rename + '">&nbsp;</em>' + LANG.rename + '</li>';
			}
			
			if( $("span#delete_file").attr("title") == "1" ){
				html += '<li id="filedelete"><em class="fa fa-lg ' + ICON.filedelete + '">&nbsp;</em>' + LANG.upload_delfile + '</li>';
			}
			
			if( html != '' ){
				html = "<ul>" + html + "</ul>";
			}
	
			$("div#contextMenu").html(html);
			NVCMENU.show(e);
		}
		
	}
	
	KEYPR.isFileSelectable = false;
}

// Ham xu ly khi click chuot vao thu muc (Xem chi tiet, dinh quyen doi ten, tao thu muc moi, xoa thu muc)
function folderMouseup( folder, e ){
	if( e.which != 3 ){ // Left mouse click
		var folderPath = $(folder).attr("title");
		if( folderPath != $("span#foldervalue").attr("title") ){
			$("span#foldervalue").attr("title", folderPath);
			$("span#view_dir").attr("title", $(folder).is(".view_dir") ? "1" : "0");
			$("span#create_dir").attr("title", $(folder).is(".create_dir") ? "1" : "0");
			$("span#rename_dir").attr("title", $(folder).is(".rename_dir") ? "1" : "0");
			$("span#delete_dir").attr("title", $(folder).is(".delete_dir") ? "1" : "0");
			$("span#upload_file").attr("title", $(folder).is(".upload_file") ? "1" : "0");
			$("span#create_file").attr("title", $(folder).is(".create_file") ? "1" : "0");
			$("span#rename_file").attr("title", $(folder).is(".rename_file") ? "1" : "0");
			$("span#delete_file").attr("title", $(folder).is(".delete_file") ? "1" : "0");
			$("span#move_file").attr("title", $(folder).is(".move_file") ? "1" : "0");
			$("span#crop_file").attr("title", $(folder).is(".crop_file") ? "1" : "0");
			$("span#rotate_file").attr("title", $(folder).is(".rotate_file") ? "1" : "0");
			$("span.folder").css("color", "");
			
			$(folder).css("color", "red");
			
			if( $(folder).is(".view_dir") ){
				var imgtype = $("select[name=imgtype]").val();
				var selFile = $("input[name=selFile]").val();
				var author = $("select[name=author]").val() == 1 ? "&author" : "";
				
				$("div#imglist").html(nv_loading_data).load(nv_module_url + "imglist&path=" + folderPath + "&imgfile=" + selFile + "&type=" + imgtype + author + "&order=" + $("select[name=order]").val() + "&random=" + nv_randomNum(10))
			}else{
				$("div#imglist").text("");
			}
			
			NVUPLOAD.init();
		}
	}else if( $(folder).is('.menu') ){ // Right mouse click
		$("span.folder").attr("name", "");
		$(folder).attr("name", "current");
		
		var html = "";
		
		if( $(folder).is(".create_dir") ){
			html += '<li id="createfolder"><em class="fa fa-lg ' + ICON.create + '">&nbsp;</em>' + LANG.createfolder + '</li>'
		}
		
		if( $(folder).is(".rename_dir") ){
			html += '<li id="renamefolder"><em class="fa fa-lg ' + ICON.rename + '">&nbsp;</em>' + LANG.renamefolder + '</li>'
		}
		
		if( $(folder).is(".delete_dir") ){
			html += '<li id="deletefolder"><em class="fa fa-lg ' + ICON.filedelete + '">&nbsp;</em>' + LANG.deletefolder + '</li>'
		}
		
		if( html != "" ){
			html = "<ul>" + html + "</ul>"
		}
		
		$("div#contextMenu").html(html);
		NVCMENU.show(e);		
	}
}

// Doi ten thu muc
function renamefolder(){
	var a = $("span[name=current]").attr("title").split("/");
	a = a[a.length - 1];
	$("input[name=foldername]").val(a);
	$("div#renamefolder").dialog("open");
}

// Tao thu muc
function createfolder(){
	$("input[name=createfoldername]").val("");
	$("div#createfolder").dialog("open")
}

// Xoa thu muc
function deletefolder(){
	if(confirm(LANG.delete_folder)){
		var a = $("span[name=current]").attr("title");
		$.ajax({
			type : "POST",
			url : nv_module_url + "delfolder&random=" + nv_randomNum(10),
			data : "path=" + a,
			success : function(b){
				b = b.split("_");
				if(b[0] == "ERROR"){
					alert(b[1])
				} else {
					b = a.split("/");
					a = "";
					for ( i = 0; i < b.length - 1; i++){
						if(a != ""){
							a += "/"
						}
						a += b[i]
					}
					b = $("select[name=imgtype]").val();
					var d = $("select[name=author]").val() == 1 ? "&author" : "", e = $("span#path").attr("title"), g = $("input[name=selFile]").val();
					$("#imgfolder").load(nv_module_url + "folderlist&path=" + e + "&currentpath=" + a + "&random=" + nv_randomNum(10));
					$("div#imglist").load(nv_module_url + "imglist&path=" + a + "&imgfile=" + g + "&type=" + b + d + "&order=" + $("select[name=order]").val() + "&order=" + $("select[name=order]").val() + "&random=" + nv_randomNum(10))
				}
			}
		})
	}
}

// Tim kiem file
function searchfile(){
	a = $("select[name=searchPath]").val(), q = $("input[name=q]").val();
	b = $("select[name=imgtype]").val(), e = $("select[name=author]").val() == 1 ? "&author" : "";
	$("div#filesearch").dialog("close");
	$("#imglist").html(nv_loading_data).load(nv_module_url + 'imglist&path=' + a + '&type=' + b + e + '&q=' + rawurlencode(q) + '&order=' + $('select[name=order]').val() + '&random=' + nv_randomNum(10))
	return false;
}

// Cat anh
function cropfile(){
	$("div.dynamic").html("");
	$("input.dynamic").val("");
	
	var selFile = $("input[name=selFile]").val();
	var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
	var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
	var size = calSize( selFileData[0], selFileData[1], 360, 360 );
	
	selFileData[0] = parseInt( selFileData[0] );
	selFileData[1] = parseInt( selFileData[1] );

	$('#cropContent').css({
		'width' : size[0] + 4,
		'height' : size[1] + 4,
	}).html('<img class="crop-image" src="' + nv_siteroot + path + "/" + selFile + '?' + selFileData[8] + '"  width="' + size[0] + '" height="' + size[1] + '"/>');

	// Check size
	if( selFileData[0] < 10 || selFileData[1] < 10 || ( selFileData[0] < 16 && selFileData[1] < 16 ) ){
		$('#cropButtons').html('<span class="text-danger">' + LANG.crop_error_small + '</span>');
	}else{
		$('#cropButtons').html(
			'X:<input type="text" id="crop-x" value="" class="w50 form-control" readonly="readonly"/> ' +
			'Y:<input type="text" id="crop-y" value="" class="w50 form-control" readonly="readonly"/> ' +
			'W:<input type="text" id="crop-w" value="" class="w50 form-control" readonly="readonly"/> ' +
			'H:<input type="text" id="crop-h" value="" class="w50 form-control" readonly="readonly"/> ' +
			'<input type="button" id="crop-save" value="' + LANG.save + '" class="btn btn-primary"/>'
		);
		
		var markScale = selFileData[0] / size[0];
		var markW = parseInt( Math.round( size[0] * 0.7 ) );
		var markH = parseInt( Math.round( size[1] * 0.7 ) );
		
		if( markW < 10 ){
			markW = 10;
		}
		
		if( markH < 10 ){
			markH = 10;
		}
		
		var markX = size[0] - markW - 5;
		var markY = size[1] - markH - 5;
		
		if( markX < 0 ){
			markX = 0;
		}
		
		if( markY < 0 ){
			markY = 0;
		}
		
		// Init watermark
		$('#cropContent img.crop-image').Watermarker({
			watermark_img : nv_siteroot + 'themes/admin_default/images/transparent.png',
			x : markX,
			y : markY,
			w : markW,
			h : markH,
			opacity : 1,
			position : 'centercenter',
			onChange : function(e){
				$('#crop-x').val( parseInt( Math.floor( markScale * e.x ) ) );
				$('#crop-y').val( parseInt( Math.floor( markScale * e.y ) ) );
				$('#crop-w').val( parseInt( Math.floor( markScale * e.w ) ) );
				$('#crop-h').val( parseInt( Math.floor( markScale * e.h ) ) );
			}
		});
		
		$('#crop-save').click(function(){
			$(this).attr('disabled', 'disabled');
			
			$.ajax({
				type : "POST",
				url : nv_module_url + "cropimg&random=" + nv_randomNum(10),
				data : "path=" + path + "&file=" + selFile + "&x=" + $('#crop-x').val() + "&y=" + $('#crop-y').val() + "&w=" + $('#crop-w').val() + "&h=" + $('#crop-h').val(),
				success : function(e){
					$('#crop-save').removeAttr('disabled');
					e = e.split('#');
					
					if( e[0] == 'ERROR' ){
						$("div#errorInfo").html(e[1]).dialog("open");
					}else{
						LFILE.reload( path, selFile );
						$("div#cropimage").dialog('close');
					}
				}
			});
		});
	}

	$("div#cropimage").dialog({
		autoOpen : false,
		width : 400,
		modal : true,
		position : "center"
	}).dialog("open");	
}

// Them logo
function addlogo(){
	$("div.dynamic").html("");
	$("input.dynamic").val("");
	
	var selFile = $("input[name=selFile]").val();
	var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
	var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
	var size = calSize( selFileData[0], selFileData[1], 360, 360 );
	var logo = $("input[name=upload_logo]").val();
	var logoConfig = $("input[name=upload_logo_config]").val().split('|');
	
	selFileData[0] = parseInt( selFileData[0] );
	selFileData[1] = parseInt( selFileData[1] );

	$('#addlogoContent').css({
		'width' : size[0] + 4,
		'height' : size[1] + 4,
	}).html('<img class="addlogo-image" src="' + nv_siteroot + path + "/" + selFile + '?' + selFileData[8] + '"  width="' + size[0] + '" height="' + size[1] + '"/>');

	// Check size
	if( selFileData[0] < 10 || selFileData[1] < 10 || ( selFileData[0] < 16 && selFileData[1] < 16 ) ){
		$('#addlogoButtons').html('<span class="text-danger">' + LANG.addlogo_error_small + '</span>');
	}else if( logo == '' ){
		$('#addlogoButtons').html('<span class="text-danger">' + LANG.notlogo + '</span>');
	}else{
		$('#addlogoButtons').html(
			'X:<input type="text" id="addlogo-x" value="" class="w50 form-control" readonly="readonly"/> ' +
			'Y:<input type="text" id="addlogo-y" value="" class="w50 form-control" readonly="readonly"/> ' +
			'W:<input type="text" id="addlogo-w" value="" class="w50 form-control" readonly="readonly"/> ' +
			'H:<input type="text" id="addlogo-h" value="" class="w50 form-control" readonly="readonly"/> ' +
			'<input type="button" id="addlogo-save" value="' + LANG.save + '" class="btn btn-primary"/>'
		);
		
		var markScale = selFileData[0] / size[0];
		
		// Set logo size
		var markW, markH;

		if( selFileData[0] <= 150 )
		{
			markW = Math.ceil( selFileData[0] * parseFloat( logoConfig[2] ) / 100 );
		}
		else if( selFileData[0] < 350 )
		{
			markW = Math.ceil( selFileData[0] * parseFloat( logoConfig[3] ) / 100 );
		}
		else
		{
			if( Math.ceil( selFileData[0] * parseFloat( logoConfig[4] ) / 100 ) > logoConfig[0] )
			{
				markW = logoConfig[0];
			}
			else
			{
				markW = Math.ceil( selFileData[0] * parseFloat( logoConfig[4] ) / 100 );
			}
		}
		
		markH = Math.ceil( markW * logoConfig[1] / logoConfig[0] );
		
		if( markH > selFileData[1] ){
			markH = selFileData[1];
			markW = Math.ceil( markH * logoConfig[0] / logoConfig[1] );
		}
		
		markW = parseInt( Math.floor( markW / markScale ) );
		markH = parseInt( Math.floor( markH / markScale ) );
		
		var markX = size[0] - markW - 5;
		var markY = size[1] - markH - 5;
		
		if( markX < 0 ){
			markX = 0;
		}
		
		if( markY < 0 ){
			markY = 0;
		}
		
		// Init watermark
		$('#addlogoContent img.addlogo-image').Watermarker({
			watermark_img : logo,
			x : markX,
			y : markY,
			w : markW,
			h : markH,
			opacity : 1,
			position : 'centercenter',
			onChange : function(e){
				$('#addlogo-x').val( parseInt( Math.floor( markScale * e.x ) ) );
				$('#addlogo-y').val( parseInt( Math.floor( markScale * e.y ) ) );
				$('#addlogo-w').val( parseInt( Math.floor( markScale * e.w ) ) );
				$('#addlogo-h').val( parseInt( Math.floor( markScale * e.h ) ) );
			}
		});
		
		$('#addlogo-save').click(function(){
			$(this).attr('disabled', 'disabled');
			
			$.ajax({
				type : "POST",
				url : nv_module_url + "addlogo&random=" + nv_randomNum(10),
				data : "path=" + path + "&file=" + selFile + "&x=" + $('#addlogo-x').val() + "&y=" + $('#addlogo-y').val() + "&w=" + $('#addlogo-w').val() + "&h=" + $('#addlogo-h').val(),
				success : function(e){
					$('#addlogo-save').removeAttr('disabled');
					e = e.split('#');
					
					if( e[0] == 'ERROR' ){
						$("div#errorInfo").html(e[1]).dialog("open");
					}else{
						LFILE.reload( path, selFile );
						$("div#addlogo").dialog('close');
					}
				}
			});
		});
	}

	$("div#addlogo").dialog({
		autoOpen : false,
		width : 400,
		modal : true,
		position : "center"
	}).dialog("open");	
}

// Xoay anh
function rotatefile(){
	$("div.dynamic").text("");
	$("input.dynamic").val("");
	$('[name="rorateDirection"]').val('0');
	
	var selFile = $("input[name=selFile]").val();
	var selFileData = $("img[title='" + selFile + "']").attr("name").split("|");
	var path = ( selFileData[7] == "" ) ? $("span#foldervalue").attr("title") : selFileData[7];
	var size = calSize( selFileData[0], selFileData[1], 360, 230 );
	
	$('#rorateimageName').html( selFile );
	$('[name="rorateFile"]').val( selFile );
	$('[name="roratePath"]').val( path );
	
	var contentMargin = parseInt( ( Math.sqrt( size[0] * size[0] + size[1] * size[1] ) - size[1] ) / 2 );
	
	$('#rorateContent').css({
		'width' : size[0],
		'height' : size[1],
		'margin-top' : contentMargin,
		'margin-bottom' : contentMargin + 10,
	}).html('<img src="' + nv_siteroot + path + "/" + selFile + '?' + selFileData[8] + '"  width="' + size[0] + '" height="' + size[1] + '"/>');

	$("div#rorateimage").dialog({
		autoOpen : false,
		width : 400,
		modal : true,
		position : "center"
	}).dialog("open");
	
	// Dat lai gia tri xoay
	RRT.direction = 0;
	RRT.currentDirection = 0;
}

// Xu ly keo tha chuot chon file
function fileSelecting(e, ui){
	if( e.ctrlKey ){
		if( $(ui.selecting).is('.imgsel') ){
			$(ui.selecting).addClass('imgtempunsel');
		}else{
			$(ui.selecting).addClass('imgtempsel');
		}
	}else if( e.shiftKey ){
		$(ui.selecting).addClass('imgtempsel');
	}else{
		$(ui.selecting).removeClass('imgtempunsel').addClass('imgtempsel');
		$('#imglist .imgcontent:not(.imgtempsel)').addClass('imgtempunsel');
	}
}

// Xu ly khi thoi chon file
function fileUnselect(e, ui){
	$(ui.unselecting).removeClass('imgtempunsel imgtempsel');
}

// Xu ly khi ket thuc chon file
function fileSelectStop(e, ui){
	$('#imglist .ui-selected').removeClass('ui-selected');
	$('.imgtempsel').addClass('imgsel').removeClass('imgtempsel');
	$('.imgtempunsel').removeClass('imgsel imgtempunsel');
	LFILE.setSelFile();
}

var ICON = [];
ICON.select = 'fa-check-square-o';
ICON.download = 'fa-download';
ICON.preview = 'fa-eye';
ICON.create = 'fa-files-o';
ICON.move = 'fa-arrows';
ICON.rename = 'fa-pencil-square-o';
ICON.filedelete = 'fa-trash-o';
ICON.filecrop = 'fa-crop';
ICON.filerotate = 'fa-repeat';
ICON.addlogo = 'fa-file-image-o';

$(".vchange").change(function(){
	var a = $("span#foldervalue").attr("title"), b = $("input[name=selFile]").val(), d = $("select[name=imgtype]").val(), e = $(this).val() == 1 ? "&author" : "";
	$("#imglist").html(nv_loading_data).load(nv_module_url + "imglist&path=" + a + "&type=" + d + "&imgfile=" + b + e + "&order=" + $("select[name=order]").val() + "&random=" + nv_randomNum(10))
});

$(".refresh em").click(function(){
	var a = $("span#foldervalue").attr("title"),
		b = $("select[name=imgtype]").val(),
		d = $("input[name=selFile]").val(),
		e = $("select[name=author]").val() == 1 ? "&author" : "",
		g = $("span#path").attr("title");
	
	$("#imgfolder").html(nv_loading_data).load(nv_module_url + "folderlist&path=" + g + "&currentpath=" + a + "&dirListRefresh&random=" + nv_randomNum(10));
	$("#imglist").html(nv_loading_data).load(nv_module_url + "imglist&path=" + a + "&type=" + b + "&imgfile=" + d + e + "&refresh&order=" + $("select[name=order]").val() + "&random=" + nv_randomNum(10));
	
	return false
});

$(".search em").click(function (){
	var a = $("span#foldervalue").attr("title"),
		b = pathList("create_file", a),
		d, e;
	$("select[name=searchPath]").html("");
	for (e in b){
		d = a == b[e] ? ' selected="selected"' : "";
		$("select[name=searchPath]").append('<option value="' + b[e] + '"' + d + ">" + b[e] + "</option>")
	}
	$("div#filesearch").dialog({
		autoOpen: false,
		width: 300,
		modal: true,
		position: "center"
	}).dialog("open");
	$("input[name=q]").val("").focus();
	return false
});

$("div#errorInfo").dialog({
	autoOpen : false,
	width : 300,
	height : 180,
	modal : true,
	position : "center",
	show : "slide"
});

$("div#renamefolder").dialog({
	autoOpen : false,
	width : 250,
	height : 160,
	modal : true,
	position : "center",
	buttons : {
		Ok : function(){
			var a = $("span[name=current]").attr("title"), b = $("input[name=foldername]").val(), d = $("span#foldervalue").attr("title"), e = a.split("/");
			e = e[e.length - 1];
			if(b == "" || b == e || !nv_namecheck.test(b)){
				alert(LANG.rename_nonamefolder);
				$("input[name=foldername]").focus();
				return false
			}
			e = $("span[name=current]").attr("class").split(" ");
			e = e[e.length - 1];
			var g = true;
			$("span." + e).each(function(){
				var h = $(this).attr("title").split("/");
				h = h[h.length - 1];
				if(b == h){
					g = false
				}
			});
			if(!g){
				alert(LANG.folder_exists);
				$("input[name=foldername]").focus();
				return false
			}
			$.ajax({
				type : "POST",
				url : nv_module_url + "renamefolder&random=" + nv_randomNum(10),
				data : "path=" + a + "&newname=" + b,
				success : function(h){
					var j = h.split("_");
					if(j[0] == "ERROR"){
						alert(j[1])
					} else {
						j = h.split("/");
						j = j[j.length - 1];
						var k;
						$("span[name=current]").parent().find("span").each(function(){
							k = $(this).attr("title");
							k = k.replace(a, h);
							$(this).attr("title") == d && $("span#foldervalue").attr("title", k);
							$(this).attr("title", k)
						});
						$("span[name=current]").html("&nbsp;" + j).attr("title", h)
					}
				}
			});
			$(this).dialog("close")
		}
	}
});

$("div#createfolder").dialog({
	autoOpen : false,
	width : 250,
	height : 160,
	modal : true,
	position : "center",
	buttons : {
		Ok : function(){
			var a = $("input[name=createfoldername]").val(), b = $("span[name=current]").attr("title");
			if(a == "" || !nv_namecheck.test(a)){
				alert(LANG.name_folder_error);
				$("input[name=createfoldername]").focus();
				return false
			}
			$.ajax({
				type : "POST",
				url : nv_module_url + "createfolder&random=" + nv_randomNum(10),
				data : "path=" + b + "&newname=" + a,
				success : function(d){
					var e = d.split("_");
					if(e[0] == "ERROR"){
						alert(e[1])
					} else {
						e = $("select[name=imgtype]").val();
						var g = $("select[name=author]").val() == 1 ? "&author" : "", h = $("span#path").attr("title");
						$("#imgfolder").load(nv_module_url + "folderlist&path=" + h + "&currentpath=" + d + "&random=" + nv_randomNum(10));
						$("div#imglist").load(nv_module_url + "imglist&path=" + d + "&type=" + e + g + "&order=" + $("select[name=order]").val() + "&random=" + nv_randomNum(10))
					}
				}
			});
			$(this).dialog("close")
		}
	}
});

$("input[name=newWidth], input[name=newHeight]").keyup(function(){
	var a = $(this).attr("name"), b = $("input[name='" + a + "']").val(), d = $("input[name=origWidth]").val(), e = $("input[name=origHeight]").val(), g = calSizeMax(d, e, nv_max_width, nv_max_height);
	g = a == "newWidth" ? g[0] : g[1];
	if(!is_numeric(b) || b > g || b < 0){
		$("input[name=newWidth]").val("");
		$("input[name=newHeight]").val("")
	} else {
		a == "newWidth" ? $("input[name=newHeight]").val(resize_byWidth(d, e, b)) : $("input[name=newWidth]").val(resize_byHeight(d, e, b))
	}
});

$("[name=prView]").click(function(){
	checkNewSize();
});

$("[name=newSizeOK]").click(function(){
	var a = $("input[name=newWidth]").val(), b = $("input[name=newHeight]").val(), d = $("input[name=origWidth]").val(), e = $("input[name=origHeight]").val();
	if(a == d && b == e){
		$("div#imgcreate").dialog("close")
	} else {
		if(checkNewSize() !== false){
			$(this).attr("disabled", "disabled");
			d = $("input[name=selFile]").val();
			var g = $("span#foldervalue").attr("title");
			$.ajax({
				type : "POST",
				url : nv_module_url + "createimg",
				data : "path=" + g + "&img=" + d + "&width=" + a + "&height=" + b + "&num=" + nv_randomNum(10),
				success : function(h){
					var j = h.split("_");
					if(j[0] == "ERROR"){
						alert(j[1]);
						$("[name=newSizeOK]").removeAttr("disabled")
					} else {
						j = $("select[name=imgtype]").val();
						var k = $("select[name=author]").val() == 1 ? "&author" : "";
						$("input[name=selFile]").val(h);
						$("[name=newSizeOK]").removeAttr("disabled");
						$("div#imgcreate").dialog("close");
						$("#imglist").load(nv_module_url + "imglist&path=" + g + "&type=" + j + "&imgfile=" + h + k + "&order=" + $("select[name=order]").val() + "&num=" + +nv_randomNum(10))
					}
				}
			})
		}
	}
});

// Di chuyen file (Luu lai)
$("input[name=newPathOK]").click(function(){
	var currentFolder = $("span#foldervalue").attr("title");
	var newPath = $("select[name=newPath]").val();
	var selFile = $("input[name=selFile]").val();
	var mirrorFile = $("input[name=mirrorFile]:checked").length;
	
	if( currentFolder == newPath ){
		$("div#filemove").dialog("close");
	}else{
		$(this).attr("disabled", "disabled");
		
		$.ajax({
			type : "POST",
			url : nv_module_url + "moveimg&num=" + nv_randomNum(10),
			data : "path=" + currentFolder + "&newpath=" + newPath + "&file=" + selFile + "&mirror=" + mirrorFile,
			success : function(e){
				var e = e.split("#");
				
				if( e[0] == "ERROR" ){
					alert( e[1] );
					$("input[name=newPathOK]").removeAttr("disabled");
				}else{
					var imgtype = $("select[name=imgtype]").val();
					var goNewPath = $("input[name=goNewPath]:checked").length;
					var author = $("select[name=author]").val() == 1 ? "&author" : "";
					var order = $("select[name=order]").val();
					var imgfile = e[1];
					
					$("input[name=newPathOK]").removeAttr("disabled");
					$("input[name=selFile]").val( imgfile );
					$("div#filemove").dialog("close");
					
					if( goNewPath == 1 ){
						goNewPath = $("span#path").attr("title");
						$("#imgfolder").load(nv_module_url + "folderlist&path=" + goNewPath + "&currentpath=" + newPath + "&random=" + nv_randomNum(10));
						$("#imglist").load(nv_module_url + "imglist&path=" + newPath + "&type=" + imgtype + "&imgfile=" + imgfile + author + "&order=" + order + "&num=" + +nv_randomNum(10));
					}else{
						$("#imglist").load(nv_module_url + "imglist&path=" + currentFolder + "&type=" + imgtype + "&imgfile=" + imgfile + author + "&order=" + order + "&num=" + +nv_randomNum(10));
					}
				}
			}
		});
	}
});

// Doi ten file (Luu lai)
$("input[name=filerenameOK]").click(function(){
	var b = $("input[name=selFile]").val(), d = $("input[name=filerenameNewName]").val(), e = b.match(/^(.+)\.([a-zA-Z0-9]+)$/);
	d = $.trim(d);
	$("input[name=filerenameNewName]").val(d);
	if(d == ""){
		alert(LANG.rename_noname);
		$("input[name=filerenameNewName]").focus();
	} else {
		a = $("input[name=filerenameAlt]").val();
		if(e[1] == d && a == $("img[title='" + b + "']").attr("alt")){
			$("div#filerename").dialog("close");
		} else {
			n = $("img[title='" + b + "']").attr("name").split("|");
			p = (n[7] == "") ? $("span#foldervalue").attr("title") : n[7];

			$(this).attr("disabled", "disabled");
			$.ajax({
				type : "POST",
				url : nv_module_url + "renameimg&num=" + nv_randomNum(10),
				data : "path=" + p + "&file=" + b + "&newname=" + d + "&newalt=" + a,
				success : function(g){
					var h = g.split("_");
					if(h[0] == "ERROR"){
						alert(h[1]);
						$("input[name=filerenameOK]").removeAttr("disabled");
					} else {
						h = $("select[name=imgtype]").val();
						var j = $("select[name=author]").val() == 1 ? "&author" : "";
						$("input[name=filerenameOK]").removeAttr("disabled");
						$("div#filerename").dialog("close");
						$("#imglist").load(nv_module_url + "imglist&path=" + p + "&type=" + h + "&imgfile=" + g + j + "&order=" + $("select[name=order]").val() + "&num=" + nv_randomNum(10));
					}
				}
			});
		}
	}
});

$("img[name=myFile2]").dblclick(function(){
	$("div[title=createInfo]").find("div").remove();
	var a = $("input[name=origWidth]").val(), b = $("input[name=origHeight]").val();
	c = calSize(a, b, 360, 230);
	$(this).width(c[0]).height(c[1]);
	$("input[name=newHeight]").val(b);
	$("input[name=newWidth]").val(a).select();
});


// Upload tu internet
function remoteUpload(){
	$("div.dynamic, span.dynamic").html("");
	$("input.dynamic").val("");
	
	$("div#uploadremote").dialog({
		autoOpen : false,
		width : 400,
		height : nv_alt_require ? 200 : 120,
		modal : true,
		position : "center"
	}).dialog("open");
	
	if( nv_auto_alt ){
		$('#uploadremoteFile').keyup(function(){
			var imageUrl = $(this).val();
		    fileAlt = nv_filename_alt( imageUrl );
			$('#uploadremoteFileAlt').val( fileAlt );
		});
	}
	
	return false;
}

// Upload tu internet (Submit)
$('[name="uploadremoteFileOK"]').click(function(){
	var fileUrl = $("input[name=uploadremoteFile]").val();
	var currUrl = $("input[name=currentFileUrl]").val();
	var folderPath = $("span#foldervalue").attr("title");
	var check = fileUrl + " " + folderPath;
	var fileAlt = $('#uploadremoteFileAlt').val();
	
	if( /^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i.test( fileUrl ) && currUrl != check && ( ( nv_alt_require && fileAlt != '' ) || ! nv_alt_require ) ){
		$(this).attr('disabled', 'disabled');
		$('#upload-remote-info').html('<em class="fa fa-2x fa-spinner fa-spin"></em>');
		
		$.ajax({
			type : "POST",
			url : nv_module_url + "upload&random=" + nv_randomNum(10),
			data : "path=" + folderPath + "&fileurl=" + fileUrl + "&filealt=" + fileAlt,
			success : function(k){
				$("input[name=currentFileUrl]").val( check );
				$('[name="uploadremoteFileOK"]').removeAttr('disabled');
				
				var l = k.split("_");
				if( l[0] == "ERROR" ){
					$("div#errorInfo").html(l[1]).dialog("open");
					$('#upload-remote-info').html('');
				}else{
					$("input[name=selFile]").val(k);
					$('#upload-remote-info').html('<em class="fa fa-2x fa-check text-success"></em>');
					LFILE.reload( folderPath, k );
					setTimeout( "NVUPLOAD.closeRemoteDialog()", 500 );
				}
			}
		});
	}else if( nv_alt_require && fileAlt == '' && fileUrl != '' ){
		$("div#errorInfo").html(LANG.upload_alt_note).dialog("open");
	}
});

/* List File Handle */
var LFILE = {
	reload : function( path, file ){
		var imgtype = $("select[name=imgtype]").val();
		var author = $("select[name=author]").val() == 1 ? "&author" : "";
		var order = $("select[name=order]").val();

		// Reset shift offset
		KEYPR.shiftOffset = 0;
		
		$("#imglist").html(nv_loading_data).load(nv_module_url + "imglist&path=" + path + "&type=" + imgtype + "&imgfile=" + file + author + "&order=" + order + "&num=" + nv_randomNum(10) );
	},
	setSelFile : function(){
		$("input[name=selFile]").val('');
	
		if( $('.imgsel').length ){
			fileName = new Array();
			$.each( $('.imgsel'), function(){
				fileName.push( $(this).attr("title") );
			});
			fileName = fileName.join('|');
			
			$("input[name=selFile]").val(fileName);
		}
	},
}

/* Rorate Handle */
var RRT = {
	direction: 0,
	currentDirection: 0,
	arrayDirection: [ 0, 90, 180, 270 ],
	timer: null,
	timeOut: 20,
	trigger: function(){
		$('#rorateContent img').rotate( RRT.direction );
	},
	setVal: function(){
		$('[name="rorateDirection"]').val( RRT.direction );
	},
	setDirection: function( direction ){
		if( direction == '' ){
			RRT.direction = 0;
		}else{
			direction = parseInt( direction );
			
			if( direction >= 360 ){
				direction = 359;
			}else if( direction < 0 ){
				direction = 0;
			}
			
			RRT.direction = direction;
		}
	},
	increase: function(){
		var direction = RRT.direction;
		direction ++;
		
		if( direction == 360 ){
			direction = 0;
		}
		
		RRT.setDirection( direction );
		RRT.setVal();
		RRT.trigger();
	},
	decrease: function(){
		var direction = RRT.direction;
		direction --;
		
		if( direction == -1 ){
			direction = 359;
		}
		
		RRT.setDirection( direction );
		RRT.setVal();
		RRT.trigger();
	},
	init: function(){
		$('[name="rorateDirection"]').keyup(function(){
			var direction = $(this).val();
			
			if( isNaN( direction ) ){
				direction = direction.slice( 0, direction.length - 1 );
			}
			
			RRT.setDirection( direction );
			RRT.setVal();
			RRT.trigger();
		});
		
		$('#rorateLeft').mousedown(function(){
			RRT.timer = setInterval( "RRT.decrease()", RRT.timeOut );
		});	
		
		$('#rorateLeft').bind( "mouseup mouseleave", function(){
			clearInterval( RRT.timer );
		});
		
		$('#rorateRight').mousedown(function(){
			RRT.timer = setInterval( "RRT.increase()", RRT.timeOut );
		});	
		
		$('#rorateRight').bind( "mouseup mouseleave", function(){
			clearInterval( RRT.timer );
		});
		
		$('#rorate90Anticlockwise').click(function(){
			RRT.currentDirection --;
			
			if( RRT.currentDirection < 0 ){
				RRT.currentDirection = 3;
			}
			
			RRT.setDirection( RRT.arrayDirection[RRT.currentDirection] );
			RRT.setVal();
			RRT.trigger();			
		});
		
		$('#rorate90Clockwise').click(function(){
			RRT.currentDirection ++;
			
			if( RRT.currentDirection > 3 ){
				RRT.currentDirection = 0;
			}
			
			RRT.setDirection( RRT.arrayDirection[RRT.currentDirection] );
			RRT.setVal();
			RRT.trigger();			
		});
		
		$('#rorateimageOK').click(function(){
			var roratePath = $('[name="roratePath"]').val();
			var rorateFile = $('[name="rorateFile"]').val();
			var rorateDirection = $('[name="rorateDirection"]').val();
			
			$(this).attr("disabled", "disabled");
			
			$.ajax({
				type : "POST",
				url : nv_module_url + "rotateimg&num=" + nv_randomNum(10),
				data : "path=" + roratePath + "&file=" + rorateFile + "&direction=" + rorateDirection,
				success : function(g){
					$('#rorateimageOK').removeAttr("disabled");
					var h = g.split("#");
					
					if( h[0] == "ERROR" ){
						alert( h[1] );
					}else{
						$("div#rorateimage").dialog("close");
						LFILE.reload( roratePath, rorateFile );
					}
				}
			});
		});
	},
};

/* Keypress, Click Handle */
var KEYPR = {
	isCtrl : false,
	isShift : false,
	shiftOffset : 0,
	allowKey : [ 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123 ],
	isSelectable: false,
	isFileSelectable: false,
	init : function(){
		$('body').keyup(function(e){			
			if( ! $(e.target).is('.dynamic') && $.inArray( e.keyCode, KEYPR.allowKey ) == -1 ){
				e.preventDefault();
			}else{
				return;
			}
			
			// Ctrl key unpress
			if( e.keyCode == 17 ){
				KEYPR.isCtrl = false;
			}else if( e.keyCode == 16 ){
				KEYPR.isShift = false;
			}
		});
		
		$('body').keydown(function(e){
			if( ! $(e.target).is('.dynamic') && $.inArray( e.keyCode, KEYPR.allowKey ) == -1 ){
				e.preventDefault();
			}else{
				return;
			}
			
			// Ctrl key press
			if( e.keyCode == 17 /* Ctrl */ ){
				KEYPR.isCtrl = true;
			}else if( e.keyCode == 27 /* ESC */ ){
				// Unselect all file
				$(".imgsel").removeClass("imgsel");
				LFILE.setSelFile();
				
				// Hide contextmenu
				NVCMENU.hide();
				
				// Reset shift offset
				KEYPR.shiftOffset = 0;
			}else if( e.keyCode == 65 /* A */ && e.ctrlKey === true ){
				// Select all file
				$(".imgcontent").addClass("imgsel");
				LFILE.setSelFile();
				
				// Hide contextmenu
				NVCMENU.hide();
			}else if( e.keyCode == 16 /* Shift */ ){
				KEYPR.isShift = true;
			}else if( e.keyCode == 46 /* Del */ ){
				// Delete file
				if( $('.imgsel').length && $("span#delete_file").attr("title") == '1' ){
					filedelete();
				}
			}else if( e.keyCode == 88 /* X */ ){
				// Move file
				if( $('.imgsel').length && $("span#move_file").attr("title") == '1' ){
					move();
				}
			}
		});
		
		// Unselect file when click on wrap area
		$('#imglist').click(function(e){
			if( KEYPR.isSelectable == false ){
				if( $(e.target).is('#imglist') ){
					$(".imgsel").removeClass("imgsel");
				}
			}
			
			KEYPR.isSelectable = false;
		});
	},
};

var NVUPLOAD = {
	uploader: null, // Pupload variable
	rendered: false, // Is rendered upload container
	started: false,
	buttons: 
		'<div class="row">' +
			'<div class="col-sm-7 buttons">' +
				'<div class="btn-group dropup browse-button">' +
					'<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown">' +
						LANG.upload_mode + ' <span class="caret"></span>' +
					'</button>' +
					'<ul class="dropdown-menu" role="menu">' +
						'<li><a id="upload-remote" onclick="return remoteUpload();" href="#">' + LANG.upload_mode_remote + '</a></li>' +
						'<li><a id="upload-local" href="#">' + LANG.upload_mode_local + '</a></li>' +
					'</ul>' +
				'</div>	' +
			'</div>' +
			'<div class="col-sm-5">' +
				'<div class="row" id="upload-queue-total">' +
					'<div class="col-sm-4 total-size"></div>' +
					'<div class="col-sm-8 total-status"></div>' +
				'</div>' +
			'</div>' +
		'</div>',	
	closeRemoteDialog : function(){
		$("div#uploadremote").dialog('close');
	},
	init : function(){
		// Reset upload if exists
		if( NVUPLOAD.uploader != null ){
			NVUPLOAD.reset();
		}
		
		// Check folder if allow upload
		var isUploadAllow = $("span#upload_file").attr("title") == "1" ? true : false;
		
		if( ! isUploadAllow ){
			$('#upload-button-area').html('<span class="text-danger"><em class="fa fa-info">&nbsp;</em>' + LANG.notupload + '</span>');
		}else{
			$('#upload-button-area').html( NVUPLOAD.buttons );
			
			var folderPath = $("span#foldervalue").attr("title");
			
			NVUPLOAD.uploader = new plupload.Uploader({
				runtimes : 'html5,flash,silverlight,html4',
				browse_button : 'upload-local',
				url : nv_module_url + "upload&path=" + folderPath + "&random=" + nv_randomNum(10),
				flash_swf_url : nv_siteroot + 'js/plupload/Moxie.swf',
				silverlight_xap_url : nv_siteroot+ 'js/plupload/Moxie.xap',
				drop_element : 'upload-content',
				file_data_name : 'upload',
				filters : nv_filters,
				multipart : true,
				init: {
					// Event on init uploader
					PostInit: function(){
						
					},
			
					// Event on add file (Add to queue or first add)
					FilesAdded: function(up, files){
						// Build upload container
						if( ! NVUPLOAD.rendered ){
							NVUPLOAD.renderUI();
						}
						
						NVUPLOAD.updateList();
						
						$('#upload-start').click(function(){
							// Check file before start upload
							var allow_start = true;
							if( nv_alt_require ){
								$.each( $('#upload-queue-files .file-alt input'), function(){
									if( $(this).val() == '' ){
										allow_start = false;
										return false;
									}
								});
								
								if( allow_start == false ){
									$("div#errorInfo").html(LANG.upload_alt_note).dialog("open");
								}
							}
							
							if( allow_start ){
								NVUPLOAD.uploader.start();
							}
						});
						
						$('#upload-cancel').click(function(){
							NVUPLOAD.uploadCancel();
						});
					},
					
					// Event on trigger a file upload status
					UploadProgress: function( up, file ){
						$('#' + file.id + ' .file-status').html( file.percent + '%' );
						NVUPLOAD.handleStatus( file, false );
						NVUPLOAD.updateTotalProgress();
					},
					
					// Event on one file finish uploaded (Maybe success or error)
					FileUploaded: function( up, file, response ){
						response = response.response;
						NVUPLOAD.handleStatus( file, response );
					},
					
					// Event on start upload or finish upload
					StateChanged: function(){
						// Start upload
						if( NVUPLOAD.uploader.state === plupload.STARTED ){
							if( ! NVUPLOAD.started ){
								NVUPLOAD.started = true;
								// Hide control button
								$('#upload-start, #upload-cancel, #upload-button-area .browse-button').hide();
								
								// Add some button
								$('#upload-button-area .buttons').append(
									'<input id="upload-stop" type="button" class="btn btn-primary" value="' + LANG.upload_stop + '"/> ' +
									'<input style="display:none" id="upload-continue" type="button" class="btn btn-primary" value="' + LANG.upload_continue + '"/>' +
									'<div class="total-info pull-right"></div>'
								);
								
								$('#upload-button-area .total-info').html(
									mOxie.sprintf( LANG.upload_info, NVUPLOAD.uploader.total.uploaded, NVUPLOAD.uploader.files.length, plupload.formatSize( NVUPLOAD.uploader.total.bytesPerSec ) )
								);
								
								// Init upload progress bar
								$('#upload-queue-total .total-status').html(
									'<div class="progress">' +
										'<div class="progress-bar" role="progressbar" aria-valuenow="' + NVUPLOAD.uploader.total.percent + '" aria-valuemin="0" aria-valuemax="100" style="width: ' + NVUPLOAD.uploader.total.percent + '%;">' + NVUPLOAD.uploader.total.percent + '%</div>' +
									'</div>'
								);
								
								// Set button handle
								$('#upload-stop').click(function(){
									$(this).hide();
									$('#upload-continue').show();
									NVUPLOAD.uploader.stop();
								});
								
								$('#upload-continue').click(function(){
									$(this).hide();
									$('#upload-stop').show();
									NVUPLOAD.uploader.start();
								});
							}
						}else{
							NVUPLOAD.updateList();
						}
					},
					
					// Event on a file is uploading
					UploadFile: function( up, file ){
						// Not thing to do
					},
					
					// Event on remove a file
					FilesRemoved: function(){
						var scrollTop = $('#upload-queue-files').scrollTop();
						NVUPLOAD.updateList();
						$('#upload-queue-files').scrollTop( scrollTop );
					},
					
					// Event on all files are uploaded
					UploadComplete: function( up, files ){
						$('#upload-continue').hide();
						$('#upload-stop').hide();
						
						// Show finish button if has failed file
						if( NVUPLOAD.uploader.total.failed > 0 ){
							$('<input type="button" class="btn btn-primary" value="' + LANG.upload_finish + '" id="upload-finish"/>').insertBefore( $('#upload-stop') );
							
							$('#upload-finish').click(function(){
								NVUPLOAD.finish();
							});
						}else{
							$('<i class="fa fa-2x text-success fa-spin fa-spinner"></i>').insertBefore( $('#upload-stop') );
							setTimeout( "NVUPLOAD.finish()", 1000 );
						}
					},
					
					// Event on error
					Error: function(up, err){
						$("div#errorInfo").html( "Error #" + err.code + ": " + err.message ).dialog("open");
						
						if( err.code === plupload.INIT_ERROR ){
							setTimeout( "NVUPLOAD.destroyUpload()", 1000 );
						}
					},
					
					// Get image alt before upload
					BeforeUpload: function(up, file) {
						var filealt = '';
						
						if( $('#' + file.id + ' .file-alt').length ){
							filealt = $('#' + file.id + ' .file-alt input').val(); 
						}
						
						NVUPLOAD.uploader.settings.multipart_params = {"filealt": filealt };
					}
				}
			});
			
			NVUPLOAD.uploader.init();
		}
	},
	renderUI: function(){
		// Hide files list and show upload container
		$('#imglist').css({'display' : 'none'});
		$('#upload-queue').css({'display' : 'block'});
		
		// Add some button
		$('#upload-button-area .buttons').append(
			'<input id="upload-start" type="button" class="btn btn-primary" value="' + LANG.upload_file + '"/> ' +
			'<input id="upload-cancel" type="button" class="btn btn-default" value="' + LANG.upload_cancel + '"/> '
		);
		
		// Change browse_button (Change style, Method: setOption is error)
		$('#upload-button-area .browse-button button').remove();
		$('#upload-remote').parent().remove();
		$('#upload-button-area .browse-button ul').removeAttr('role').removeClass('dropdown-menu').addClass('fixul');
		$('#upload-local').addClass('btn btn-primary').text(LANG.upload_add_files);
		$('#upload-button-area .browse-button ul li div:first').width( $('#upload-local').outerWidth() ).height( $('#upload-local').outerHeight() );
		
		// Build upload queue
		$('#upload-queue').html('\
			<div class="queue-header">\
				<div class="container-fluid">\
					<div class="row">\
						<div class="col-sm-' + ( nv_alt_require ? '4' : '7' ) + '">' + LANG.file_name + '</div>\
						' + ( nv_alt_require ? '<div class="col-sm-3">' + LANG.altimage + '</div>' : '' ) + '\
						<div class="col-sm-2">' + LANG.upload_size + '</div>\
						<div class="col-sm-3">' + LANG.upload_status + '</div>\
					</div>\
				</div>\
			</div>\
			<div id="upload-queue-files" class="container-fluid"></div>\
		');
		
		// Rendered is true
		NVUPLOAD.rendered = true;
	},
	updateList: function(){
		var fileList = $('#upload-queue-files').html('');
		var fileAlt;

		$.each( NVUPLOAD.uploader.files, function(i, file){
			if( ! nv_alt_require ){
				fileList.append(
					'<div id="' + file.id + '" class="row file-item">' +
						'<div class="col-sm-7 file-name"><span>' + file.name + '</span></div>' +
						'<div class="col-sm-2 file-size">' + plupload.formatSize(file.size) + '</div>' +
						'<div class="col-sm-2 file-status">' + file.percent + '%</div>' +
						'<div class="col-sm-1 file-action text-right"></div>' +
					'</div>'
				);			
			}else{
				fileAlt = NVLDATA.getValue(file.id);
				
				if( nv_auto_alt && fileAlt == '' ){

				    fileAlt = nv_filename_alt( file.name );
					
					NVLDATA.setValue( file.id, fileAlt );					
				}
				
				fileList.append(
					'<div id="' + file.id + '" class="row file-item">' +
						'<div class="col-sm-4 file-name"><span>' + file.name + '</span></div>' +
						'<div class="col-sm-3 file-alt"><input type="text" value="' + fileAlt + '" onkeyup="NVLDATA.setValue( \'' + file.id + '\', this.value);" class="form-control upload-file-alt dynamic"/></div>' +
						'<div class="col-sm-2 file-size">' + plupload.formatSize(file.size) + '</div>' +
						'<div class="col-sm-2 file-status">' + file.percent + '%</div>' +
						'<div class="col-sm-1 file-action text-right"></div>' +
					'</div>'
				);			
			}
			
			NVUPLOAD.handleStatus( file, false );

			$('#' + file.id + ' .file-delete').click(function(e){
				$('#' + file.id).remove();
				NVUPLOAD.uploader.removeFile( file );

				e.preventDefault();
			});
		});

		$('#upload-queue-total .total-size').html( plupload.formatSize( NVUPLOAD.uploader.total.size ) );

		// Scroll to end of file list
		fileList[0].scrollTop = fileList[0].scrollHeight;

		NVUPLOAD.updateTotalProgress();
		
		// Enable, disable start button
		if( NVUPLOAD.uploader.files.length ){
			$('#upload-start').removeAttr('disabled');
		}else{
			$('#upload-start').attr('disabled', 'disabled');
		}
	},
	reset: function(){
		// Destroy current uploader
		NVUPLOAD.uploader.destroy();
		NVUPLOAD.started = false;
		
		// Clear uploader variable
		NVUPLOAD.uploader = null;
		
		// Reset upload button
		$('#upload-button-area').html( NVUPLOAD.buttons );
		
		// Clear upload container
		$('#upload-queue-files').html('');
	},
	uploadCancel: function(){
		// Reset uploader
		NVUPLOAD.reset();
		
		// Hide upload container and show file list
		$('#upload-queue').html('').css({'display' : 'none'});
		$('#imglist').css({'display' : 'block'});
		
		// Rendered is false
		NVUPLOAD.rendered = false;
		
		// Init uploader
		NVUPLOAD.init();
	},
	destroyUpload: function(){
		// Reset uploader
		NVUPLOAD.reset();
		
		// Hide upload container and show file list
		$('#upload-queue').html('').css({'display' : 'none'});
		$('#imglist').css({'display' : 'block'});
		
		// Rendered is false
		NVUPLOAD.rendered = false;
	},
	handleStatus: function( file, response ){
		var actionClass;
		
		if( response != false ){
			check = response.split('_');
			
			if( check[0] == 'ERROR' ){
				file.status = plupload.FAILED;
				file.hint = check[1];
				NVUPLOAD.uploader.total.uploaded --;
				NVUPLOAD.uploader.total.failed ++;			
			}else{
				file.name = response;
			}
			
			$.each( NVUPLOAD.uploader.files, function(i, f){
				if( f.id == file.id ){
					NVUPLOAD.uploader.files[i].status = file.status;
					NVUPLOAD.uploader.files[i].hint = file.hint;
					NVUPLOAD.uploader.files[i].name = file.name;
				}
			});
		}
		
		if( file.status == plupload.DONE ){
			actionClass = 'text-success fa fa-lg fa-check';
		}else if( file.status == plupload.FAILED ){
			actionClass = 'text-danger fa fa-lg fa-exclamation-triangle';
		}else if( file.status == plupload.QUEUED ){
			actionClass = 'fa fa-lg fa-trash-o file-delete fa-pointer';
		}else if( file.status == plupload.UPLOADING ){
			actionClass = 'text-info fa fa-lg fa-spin fa-circle-o-notch';
		}else{
			// Nothing to do
		}

		$('#' + file.id + ' .file-action').html('<i class="' + actionClass + '"></i>');
		
		if( file.hint ){
			$('#' + file.id).attr('title', file.hint);	
		}
	},
	updateTotalProgress: function(){
		$('#upload-queue-total .total-status').html(
			'<div class="progress">' +
				'<div class="progress-bar" role="progressbar" aria-valuenow="' + NVUPLOAD.uploader.total.percent + '" aria-valuemin="0" aria-valuemax="100" style="width: ' + NVUPLOAD.uploader.total.percent + '%;">' + NVUPLOAD.uploader.total.percent + '%</div>' +
			'</div>'
		);
		
		$('#upload-button-area .total-info').html(
			mOxie.sprintf( LANG.upload_info, NVUPLOAD.uploader.total.uploaded, NVUPLOAD.uploader.files.length, plupload.formatSize( NVUPLOAD.uploader.total.bytesPerSec ) )
		);
	},
	finish: function(){
		var folderPath = $("span#foldervalue").attr("title");
		
		if( NVUPLOAD.uploader.total.uploaded > 0 ){
			var selFile = new Array();
			
			$.each( NVUPLOAD.uploader.files, function( k, v ){
				if( v.status == plupload.DONE ){
					selFile.push( v.name );
				}
			});
			
			selFile = selFile.join('|');
		}else{
			var selFile = '';
		}

		$("input[name=selFile]").val( selFile );
		NVUPLOAD.uploadCancel();
		LFILE.reload( folderPath, selFile );
	},
};

var NVCMENU = {
    menuStyle: {
      listStyle: 'none',
      padding: '1px',
      margin: '0px',
      backgroundColor: '#fff',
      border: '1px solid #999',
      width: '120px'
    },
    itemStyle: {
      margin: '0px',
      color: '#000',
      display: 'block',
      cursor: 'default',
      padding: '3px',
      border: '1px solid #fff',
      backgroundColor: 'transparent'
    },
    itemHoverStyle: {
      border: '1px solid #0a246a',
      backgroundColor: '#b6bdd2'
    },
    shadow : null,
    menu : null,
	bindings : {
		select : function() {
			insertvaluetofield();
		},
		download : function() {
			download();
		},
		filepreview : function() {
			preview();
		},
		fileaddlogo : function() {
			addlogo();
		},
		create : function() {
			create();
		},
		move : function() {
			move();
		},
		rename : function() {
			filerename();
		},
		filedelete : function() {
			filedelete();
		},
        cropfile : function() {
			cropfile();
		},
		rotatefile : function() {
			rotatefile();
		},
		renamefolder : function() {
			renamefolder()
		},
		createfolder : function() {
			createfolder()
		},
		deletefolder : function() {
			deletefolder()
		}
	},
	init : function(){
		NVCMENU.menu = $('<div id="nvContextMenu"></div>').hide().css({position:'absolute', zIndex:'500'}).appendTo('body').bind('click', function(e){
			e.stopPropagation();
		});
		NVCMENU.shadow = $('<div id="nvContextMenuShadow"></div>').hide().css({backgroundColor:'#000',position:'absolute',opacity:0.2,zIndex:499}).appendTo('body');
		
		$(document).delegate( '*', 'click', function(e){
			if( e.which != 3 ){
				NVCMENU.hide();
			}	
		});
	},
	show : function(e){
		e.preventDefault();
		
		if( $('#contextMenu').html() != '' ){
			var content = $('#contextMenu').find('ul:first').clone(true);
		    content.css(NVCMENU.menuStyle).find('li').css(NVCMENU.itemStyle).hover(function(){
		        $(this).css(NVCMENU.itemHoverStyle);
		      },
		      function(){
		        $(this).css(NVCMENU.itemStyle);
		      }
		    ).find('img').css({verticalAlign:'middle',paddingRight:'2px'});
		    
		   	NVCMENU.menu.html(content);
		    
			$.each(NVCMENU.bindings, function(id, func){
				$('#' + id, NVCMENU.menu).bind('click', function(e){
					NVCMENU.hide();
					func();
				});
			});

			NVCMENU.menu.css({ 'left' : e.pageX + 1, 'top' : e.pageY + 1 }).show();
			NVCMENU.shadow.css({ 'width' : NVCMENU.menu.width(), 'height' : NVCMENU.menu.height(), 'left' : e.pageX + 3, 'top' : e.pageY + 3 }).show();			
		}
		return false;
	},
	hide : function(){
		NVCMENU.menu.hide();
		NVCMENU.shadow.hide();
	},
};

var NVLDATA = {
	support : false,
	init : function(){
		if( typeof( Storage ) !== "undefined" ){
			NVLDATA.support = true;
		}
	},
	getValue : function( key ){
		if( ! NVLDATA.support ){
			return '';
		}
		
		if( typeof( sessionStorage[key] ) !== "undefined" && sessionStorage[key] ){
			return sessionStorage[key];
		}
		
		return '';
	},
	setValue : function( key, val ){
		sessionStorage[key] = val;
	}
};

// Init functions
KEYPR.init();
RRT.init();
NVCMENU.init();
NVLDATA.init();